package cn.edu.scau.cmi.chenxiaoqin.client;

import java.util.Iterator;
import java.util.Set;

import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.chenxiaoqin.hibernate.dao.StudentDao;
import cn.edu.scau.cmi.chenxiaoqin.hibernate.dao.TeacherDao;
import cn.edu.scau.cmi.chenxiaoqin.hibernate.domain.Student;
import cn.edu.scau.cmi.chenxiaoqin.hibernate.domain.Teacher;
import cn.edu.scau.cmi.chenxiaoqin.spring.util.ApplicationContextUtil;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class SpringClient extends Application{

	static ApplicationContext ac=getApplicationContext(); 
	public static void main(String[] args) {
	
		// TODO Auto-generated method stub
          launch(args);
	}
	
	private static ApplicationContext getApplicationContext() {
		ApplicationContext applicationContext=ApplicationContextUtil.getApplication();
		return applicationContext;
	}
	public void start(Stage stage) {
		
		FlowPane allPane=new FlowPane();
		allPane.setPadding(new Insets(10, 20, 10, 20));
		allPane.setHgap(15);
		allPane.setVgap(20);
		allPane.setAlignment(Pos.BASELINE_CENTER);
		Label label = new Label("提示：");
//		TextArea outArea=new TextArea();
//		allPane.getChildren().add(outArea);
		
		Label typeLabel=new Label("学生"); //类型:学生
		typeLabel.setPrefWidth(700);
		typeLabel.setFont(Font.font("Timer New Roman",FontWeight.BOLD, FontPosture.ITALIC, 40));
		//allPane.getChildren().add(typeLabel);
		
		Label studenId=new Label("学号:");  //学生ID
		TextField stuIdIn=new TextField();  //学生ID输入框
		stuIdIn.setPrefWidth(250);
		Label studenName=new Label("姓名:");//学生姓名
		TextField stuNaI=new TextField(); //学生姓名输入框
	    stuNaI.setPrefWidth(250);
		//allPane.getChildren().addAll(studenId,stuIdIn,studenName,stuNaI);
	    FlowPane pane1 =new FlowPane();
	    pane1.setPadding(new Insets(10, 20, 10, 20));
	    pane1.setHgap(15);
		pane1.getChildren().addAll(studenId,stuIdIn,studenName,stuNaI);
		
		Button buttonOne=new Button("增加"); //增加学生ID
		buttonOne.setPrefWidth(180);
		buttonOne.setOnAction(e->{
			StudentDao stuDao=(StudentDao)ac.getBean("studentDao");
			Student stu=new Student();
			String id=stuIdIn.getText();  //获取id和姓名
			String name=stuNaI.getText();
			Student comp=stuDao.getById(id);
			if(comp==null&&id!=null) {
			stu.setId(id);
			stu.setName(name);
			stuDao.save(stu);
			label.setText("提示：添加学生信息成功！");
			System.out.println("添加成功!");
			} //test
		});
		
		Button buttonTwo=new Button("修改"); //修改学生姓名
		buttonTwo.setPrefWidth(180);
		buttonTwo.setOnAction(e->{
			StudentDao stuDao=(StudentDao)ac.getBean("studentDao");
			String id=stuIdIn.getText();  
			String name=stuNaI.getText();
			Student comp=stuDao.getById(id);//获取id和姓名
			System.out.print(1);
			if(comp!=null&&id!=null) { 
			comp.setName(name);
			stuDao.update(comp);
			label.setText("提示：修改学生信息成功！");
			System.out.println("修改成功!");} //test
		});
		
		Button buttonThree=new Button("删除"); //删除学生信息
		buttonThree.setPrefWidth(180);
		buttonThree.setOnAction(e->{
			StudentDao stutDao=(StudentDao)ac.getBean("studentDao");
			String id=stuIdIn.getText();  
			String name=stuNaI.getText();
			StudentDao stuDao=new StudentDao();
			Student comp=stuDao.getById(id);
			if(comp!=null) { 
			stuDao.delete(comp);
			label.setText("提示：删除学生信息成功！");
			System.out.println("删除成功!"); } //test
		});
		FlowPane pane2 =new FlowPane();
		pane2.setPadding(new Insets(10, 20, 10, 20));
		pane2.setHgap(15);
		pane2.getChildren().addAll(buttonOne,buttonTwo,buttonThree);
		
		Label seleId=new Label("老师工号:");  //学生选老师ID
		seleId.setPrefWidth(80);
		TextField seleIdIn=new TextField();  //学生选老师ID输入框
		//allPane.getChildren().addAll(seleId,seleIdIn);
		seleIdIn.setPrefWidth(250);
		Button buttonFour=new Button("选择"); //学生选老师ID
		buttonFour.setPrefWidth(100);
		buttonFour.setOnAction(e->{
			StudentDao stuDao=(StudentDao)ac.getBean("studentDao");
			TeacherDao teaDao=(TeacherDao)ac.getBean("teacherDao");
			Teacher tea=null;
			Student stu=stuDao.getById(stuIdIn.getText());
			if(stu!=null) {
				tea=teaDao.getById(seleIdIn.getText());
				Set<Student> sL=tea.getStuList();
				Iterator<?> sLI=sL.iterator();
				if(tea!=null)
				{
					stu.setTutor(tea);
					stuDao.select(stu);
					sL.add(stu);
					tea.setStuList(sL);
					teaDao.update(tea);
					label.setText("提示：学生选择老师成功！");
					System.out.print("选择成功");
				}
			}
			else {
				System.out.print("选择失败");
			}
		});
		//allPane.getChildren().addAll(seleId,seleIdIn,buttonFour);
		
		Button buttonFive=new Button("修改"); //学生修改老师ID
		buttonFive.setPrefWidth(100);
		buttonFive.setOnAction(e->{
			StudentDao stuDao=(StudentDao)ac.getBean("studentDao");
			TeacherDao teaDao=(TeacherDao)ac.getBean("teacherDao");
			String teaI=seleIdIn.getText();
			String stuI=stuIdIn.getText();
			Student stu=stuDao.getById(stuI);
			Teacher tea=teaDao.getById(teaI);
			Set<Student> sL=tea.getStuList();
			if(stu!=null&&tea!=null) {
				stu.setTutor(tea);
				stuDao.update(stu);
				sL.add(stu);
				tea.setStuList(sL);
				teaDao.update(tea);
				label.setText("提示：学生修改老师成功！");
				System.out.println("修改成功");
			}
		});
		//allPane.getChildren().add(buttonFive);
		FlowPane pane3 =new FlowPane();
		pane3.setPadding(new Insets(10, 20, 10, 20));
		pane3.setHgap(15);
		pane3.getChildren().addAll(seleId,seleIdIn,buttonFour,buttonFive);
		
		Label typeT=new Label("老师"); //类型: 老师
		 typeT.setPrefWidth(700);
		 typeT.setFont(Font.font("Timer New Roman",FontWeight.BOLD, FontPosture.ITALIC, 40));
		//allPane.getChildren().add(typeT);
		
		Label teaId=new Label("工号:");  //老师ID
		TextField teaIdIn=new TextField();  //老师ID输入框
		teaIdIn.setPrefWidth(250);
		Label teaName=new Label("姓名:");//老师姓名
		TextField teaNaI=new TextField(); //老师姓名输入框
		teaNaI.setPrefWidth(250);
		//allPane.getChildren().addAll(teaId,teaIdIn,teaName,teaNaI);
		FlowPane pane4 =new FlowPane();
		pane4.setPadding(new Insets(10, 20, 10, 20));
		pane4.setHgap(15);
		pane4.getChildren().addAll(teaId,teaIdIn,teaName,teaNaI);
		
		Button buttonSive=new Button("增加"); //增加老师ID
		buttonSive.setPrefWidth(180);
		buttonSive.setOnAction(e->{
			TeacherDao teaDao=(TeacherDao)ac.getBean("teacherDao");
			Teacher tea=new Teacher();
			String id=teaIdIn.getText();  //获取id和姓名
			String name=teaNaI.getText();
			Teacher comp=teaDao.getById(id);
			if(comp==null&&id!=null) {
			tea.setId(id);
			tea.setName(name);
			teaDao.save(tea);
			label.setText("提示：添加老师信息成功！");
			System.out.println("添加成功!");
			} //test
		});
		
		Button buttonSev=new Button("修改"); //修改老师姓名
		buttonSev.setPrefWidth(180);
		buttonSev.setOnAction(e->{
			TeacherDao teaDao=(TeacherDao)ac.getBean("teacherDao");
			Teacher tea=new Teacher();
			String id=teaIdIn.getText();  //获取id和姓名
			String name=teaNaI.getText();
			Teacher comp=teaDao.getById(id);
			if(comp!=null&&id!=null&&name!=null) {
			comp.setName(name);
			teaDao.update(comp);
			label.setText("提示：修改老师信息成功！");
			System.out.println("修改成功!");
			} //test
		});
		
		Button buttonEig=new Button("删除"); //删除老师信息
		buttonEig.setPrefWidth(200);
		buttonEig.setOnAction(e->{
			String id=teaIdIn.getText();  
			String name=teaNaI.getText();
			Teacher tea=new Teacher();
			TeacherDao teaDao=new TeacherDao();
			Teacher comp=teaDao.getById(id);
			Teacher stu=new Teacher();
			if(comp!=null&&id!=null) { 
			teaDao.delete(comp);
			label.setText("提示：删除老师信息成功！");
			System.out.println("删除成功!"); } //test
		});
		FlowPane pane5 =new FlowPane();
		pane5.setPadding(new Insets(10, 20, 10, 20));
		pane5.setHgap(15);
		pane5.getChildren().addAll(buttonSive,buttonSev,buttonEig);
		
		
		Label stuSeId=new Label("学生学号:");  //老师选学生ID
		seleId.setPrefWidth(80);
		TextField stuSeIdIn=new TextField();  //老师选学生ID输入框
		seleIdIn.setPrefWidth(250);
		Button buttonNin=new Button("选择"); //学生ID
		buttonNin.setPrefWidth(100);
		buttonNin.setOnAction(e->{
			StudentDao stuDao=(StudentDao)ac.getBean("studentDao");
			TeacherDao teaDao=(TeacherDao)ac.getBean("teacherDao");
			Teacher tea=teaDao.getById(teaIdIn.getText());
			Student stu=stuDao.getById(stuSeIdIn.getText());
			label.setText("提示：老师选择学生成功！");
			if(stu!=null&&tea!=null) {
				System.out.print(stu.getId());
				
				    Set<Student> temp=tea.getStuList();
				    temp.add(stu);
				    tea.setStuList(temp);
					stu.setTutor(tea);
					teaDao.select(tea);
					stuDao.update(stu);
					
					System.out.print("选择成功");
			}
			else {
				System.out.print("选择失败");
			}
		});
		Button buttonTen=new Button("修改"); //学生ID
		buttonTen.setPrefWidth(100);
		buttonTen.setOnAction(e->{
			label.setText("提示：老师修改学生成功！");
			StudentDao stuDao=(StudentDao)ac.getBean("studentDao");
			TeacherDao teaDao=(TeacherDao)ac.getBean("teacherDao");
			Teacher tea=teaDao.getById(teaIdIn.getText());
			Student stu=stuDao.getById(stuSeIdIn.getText());
			if(stu!=null&&tea!=null) {
				System.out.print(stu.getId());
				
				    Set<Student> temp=tea.getStuList();
				    temp.add(stu);
				    tea.setStuList(temp);
					stu.setTutor(tea);
					teaDao.select(tea);
					stuDao.update(stu);
					System.out.print("修改成功");
			}
			else {
				System.out.print("修改选择失败");
			}
		});
		FlowPane pane6 =new FlowPane();
		pane6.setPadding(new Insets(10, 20, 10, 20));
		pane6.setHgap(15);
		pane6.getChildren().addAll(stuSeId,stuSeIdIn,buttonNin,buttonTen);
		VBox box = new VBox();
		box.setPadding(new Insets(10, 20, 10, 20));
		
		box.getChildren().addAll(typeLabel,pane1,pane2,pane3,typeT,pane4,pane5,pane6,label);
		Scene scence=new Scene(box,700,800);
		stage.setScene(scence);
		stage.show();
	}
}

